<template>
  <scroll class="listview" listenScroll :probeType="3" ref="scrollRef"
  :data="data"
  @scroll="scroll">
    <!-- 左边歌手列表 -->
    <ul>
      <li v-for="group in data" ref="listGroup" :key="group.title" class="list-group">
        <h1 class="list-group-title">{{group.title}}</h1>
        <ul>
          <li @click="select(item)" v-for="item in group.items" class="list-group-item" :key="item.id">
            <img class="avatar" v-lazy="item.avatar" alt="">
            <span class="name">{{item.name}}</span>
          </li>
        </ul>
      </li>
    </ul>
    <!-- 右边快速索引 -->
    <div class="shortcut-list">
      <ul>
        <li v-for="(item,index) in shortcutList"
        :class="{active:index === currentIndex}"
        class="shortcut-list-item"
        :key="item"
        @touchstart="onShortcutTouchStart(index,$event)"
        @touchmove.stop.prevent="onShortcutTouchMove">
          {{item}}
        </li>
      </ul>
    </div>
    <!-- 悬停div -->
    <div v-show="fixedTitle" class="list-fixed" ref="fixedRef">
      <h1 class="list-fixed-title">{{fixedTitle}}</h1>
    </div>
    <!-- Loading组件 -->
    <div v-if="!data.length" class="loading-container">
      <loading></loading>
    </div>
  </scroll>
</template>

<style lang="stylus" scoped>
  @import "~common/stylus/variable"
  .listview
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: $color-background;
    .list-group
      padding-bottom 30px
      &-title
        font-size $font-size-small
        height 30px
        line-height 30px
        padding-left 20px
        color $color-text-l
        background $color-highlight-background
      &-item
        display flex
        align-items center
        padding 20px 0 0 30px
        .avatar
          width 50px
          height 50px
          border-radius 50%
        .name
          margin-left 20px
          color $color-text-l
          font-size $font-size-medium
    .shortcut-list
      position absolute
      z-index 30
      right 0
      width 30px
      // height 300px
      top 50%
      transform translateY(-50%)
      background $color-highlight-background
      font-size $font-size-medium
      text-align center
      padding 5px 0px 10px 0px
      border-radius 35px
      &-item
        margin-top 5px
      .active
        color $color-theme
    .list-fixed
      position absolute
      left 0
      top 0
      width 100%
      &-title
        font-size $font-size-small
        height 30px
        line-height 30px
        padding-left 20px
        color $color-text-l
        background $color-highlight-background
    .loading-container
      position absolute
      top 50%
      left 0
      width 100%
      transform translateY(-50%)
</style>


<script>
  import Scroll from 'base/scroll/scroll'
  import Loading from 'base/loading/loading'

  const ANCHOR_HEIGHT = 18
  const TITLE_HEIGHT = 20

  export default {
    created() {
      // this.listGroupItems = []
      this.touch = {}
    },
    data() {
      return {
        scrollY : 0,
        currentIndex : 0,
        diff : -1
      }
    },
    props:{
      data : {
        type : Array,
        default : []
      }
    },
    components:{Scroll, Loading},
    updated() {
      // setTimeout(() => {
      //   this.listGroupItems = this.$refs.listGroup
      // }, 20)
    },
    // mounted() {
    //   setTimeout(() => {
    //     this.itemsHeight = []
    //     this.itemY = 0
    //     let height = 0
    //     this.$refs.listGroup.forEach(item => {
    //       this.itemsHeight.push(item)
    //       height += item.clientHeight
    //       item.itemY = height

    //       console.log(item.itemY)
    //     })

    //     console.log(this.itemsHeight)
    //   }, 200)
    // },
    computed:{
      shortcutList() {
        return this.data.map(item => {
          return item.title.substring(0, 1)
        })
      },
      fixedTitle() {
        if (this.scrollY > 0) {
          return ''
        }
        return this.data.length > 0 ? this.data[this.currentIndex].title : ''
      }
    },
    watch:{
      data() {
        setTimeout(() => {
          this._calcHeight()
        }, 20)
      },
      scrollY(newY) {
        // console.log(newY)
        // for (var i = 0; i < this.itemsHeight.length; i++) {
        //   const item1 = this.itemsHeight[i]
        //   const item2 = this.itemsHeight[i + 1]

        //   console.log(item1.itemY)
        //   console.log(item2.itemY)

        //   // if (item1.itemY < -newY < item2.itemY) {
        //   //   console.log(i)
        //   // }
        // }
        const listHeight = this.listHeight
        for (var i = 0; i < listHeight.length; i++) {
          const height1 = listHeight[i]
          const height2 = listHeight[i + 1]

          if (height1 < -newY && -newY < height2) {
            this.currentIndex = i
            this.diff = height2 + newY
            return
          }
        }
      },
      diff(newValue) {
        let fixedTop = (newValue > 0 && newValue < TITLE_HEIGHT) ? newValue - TITLE_HEIGHT : 0

        // console.log(fixedTop)
        if (fixedTop === this.fixedTop) {
          return
        }

        this.fixedTop = fixedTop
        this.$refs.fixedRef.style.transform = `translate3d(0,${fixedTop}px,0)`
      }
    },
    methods:{
      onShortcutTouchStart(index, e) {
        // this.$refs.scrollRef.getScroll().this.$refs.scrollRef.(this.listGroupItems[index], 0)
        // 手指按下的第一个点
        // console.log(index)
        const firstTouch = e.touches[0]
        this.touch.y1 = firstTouch.pageY
        this.touch.anchorIndex = index

        this._scrollTo(index)
      },
      onShortcutTouchMove(e) {
        const firstTouch = e.touches[0]

        this.touch.y2 = firstTouch.pageY

        const delta = (this.touch.y2 - this.touch.y1) / ANCHOR_HEIGHT | 0

        const anchorIndex = parseInt(this.touch.anchorIndex) + delta

        this._scrollTo(anchorIndex)
      },
      scroll(pos) {
        this.scrollY = pos.y
      },
      select(item) {
        this.$emit('select', item)
      },
      _scrollTo(index) {
        this.$refs.scrollRef.scrollToElement(this.$refs.listGroup[index])

        this.currentIndex = index
      },
      _calcHeight() {
        this.listHeight = []
        let height = 0
        this.listHeight.push(height)
        this.$refs.listGroup.forEach(item => {
          height += item.clientHeight
          this.listHeight.push(height)
        })

        // console.log(this.listHeight)
      }
    }
  }
</script>
